package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;
    private final ShoppingCartMapper shoppingCartMapper;

    //添加购物车
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Boolean dishExists = shoppingCartDTO.getDishId() != null;
        //1.查询是否有已存在菜品
        ShoppingCart shoppingCart = shoppingCartMapper.getById(shoppingCartDTO, BaseContext.getCurrentId());
        if(shoppingCart != null) {
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateShoppingCart(shoppingCart);
            return;
        }
        //2.补全购物车内容
        shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        if(dishExists){
            DishVO dishVo=dishMapper.selectById(shoppingCart.getDishId());
            shoppingCart.setAmount(dishVo.getPrice());
            shoppingCart.setName(dishVo.getName());
            shoppingCart.setImage(dishVo.getImage());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
        }
        else {
            SetmealVO setmealVO=setmealMapper.selectSetmealById(shoppingCart.getSetmealId());
            shoppingCart.setAmount(setmealVO.getPrice());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
        }
        shoppingCartMapper.insertShoppingCart(shoppingCart);
    }

    //查看购物车
    public List<ShoppingCart> listShoppingCart() {
        return shoppingCartMapper.getListByUserId(BaseContext.getCurrentId());
    }

    //删除购物车中一个商品
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.获取购物车菜品信息
        ShoppingCart shoppingCart = shoppingCartMapper.getById(shoppingCartDTO, BaseContext.getCurrentId());
        if(shoppingCart == null) return;
        //2.如果只剩一份，则删除
        if (shoppingCart.getNumber() <= 1) {
            shoppingCartMapper.deleteShoppingCart(shoppingCart);
            return;
        }
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.updateShoppingCart(shoppingCart);
    }

    //清空购物车
    public void cleanShoppingCart() {
        shoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
    }

}
